﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Sale_Operation;
using Sale_Common;
using SaleSystem;
using Sale_Model;
namespace SaleSystem.BaseData
{
    public partial class frm_BaseDataList : PageBase
    {
        #region PageEvent
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                bnNew.OnClientClick = win_Content.GetShowReference("frm_BaseDataEdit.aspx?Id=0", "新增字典信息");
                BindDropDownList();
                GetAndBind();
            }
            else if (Request[LinkService.RefreshTag] == LinkService.RefreshString)
            {
                GetAndBind();                
            }
        }
        protected void bnSearch_Click(object sender, EventArgs e)
        {
            GetAndBind();
        }
        protected void gv_List_RowDataBound(object sender, ExtAspNet.GridRowEventArgs e)
        {
             
        }
        protected void gv_List_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "delete")
            {
                Sale_Model.BaseData SM = new Sale_Model.BaseData();

                SM.BData_Id = ValueHandler.GetIntNumberValue(gv_List.DataKeys[e.RowIndex][0]);
                SM.BData_Name = ValueHandler.GetStringValue(gv_List.DataKeys[e.RowIndex][1]);
                bool blIsUsing = LinkService.CheckIsUsing("BaseData1", Id.ToString());
                if (!blIsUsing)
                {

                    bool blDelete = Manager.Delete(SM);
                    if (blDelete)
                    {
                        GetAndBind();
                        ExtAspNet.Alert.Show("删除完成！", ExtAspNet.MessageBoxIcon.Information);
                    }
                    else
                        ExtAspNet.Alert.Show("删除失败！", ExtAspNet.MessageBoxIcon.Error);
                }
                else
                    ExtAspNet.Alert.Show("该数据项正在使用，不能删除！", ExtAspNet.MessageBoxIcon.Warning);
            }
            else if(e.CommandName.ToLower()=="edit")
            {
                win_Content.Hidden = false;
                win_Content.IFrameUrl = "frm_BaseDataEdit.aspx?Id=" + ValueHandler.GetIntNumberValue(gv_List.DataKeys[e.RowIndex][0]);
                win_Content.Title = "编辑-" + ValueHandler.GetStringValue(gv_List.DataKeys[e.RowIndex][1]);
            }
        }
        protected void gv_List_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            gv_List.PageIndex = e.NewPageIndex;
            GetAndBind();
        }
        #endregion

        #region Method
        private void BindDropDownList()
        {
            DataTable dtType= Manager.GetTypeList();
            ddl_BData_Type.DataSource = dtType;
            ddl_BData_Type.DataBind();
            ddl_BData_Type.Items.Insert(0, new ExtAspNet.ListItem("-未选择-", "-1"));
        }
        private void GetAndBind()
        {
            FormatGrid(gv_List);
            InitCondition();
            dtList = Manager.GetList(strCondition,gv_List.PageIndex+1,ref iRecordCount);
            gv_List.DataSource = dtList;
            gv_List.DataBind();
            gv_List.RecordCount = iRecordCount;
        }
        /// <summary>
        /// 获取查询条件
        /// </summary>
        public override void InitCondition()
        {
            strCondition = "";
            if (tb_BData_Code.Text.Trim() != "")
            {
                strCondition += " BData_Code LIKE '%" + ValueHandler.GetStringValue(tb_BData_Code.Text, ValueHandler.CharHandlerType.IsRepChar) + "%' AND ";
            }
            if (tb_BData_Name.Text.Trim() != "")
            {
                strCondition += " BData_Name LIKE '%" + ValueHandler.GetStringValue(tb_BData_Name.Text, ValueHandler.CharHandlerType.IsRepChar) + "%' AND ";                
            }
            if (ddl_BData_Type.SelectedValue != "-1")
            {
                strCondition += " BData_Type ='" + ddl_BData_Type.SelectedValue + "' AND ";
            }
            if (!string.IsNullOrEmpty(strCondition))
                strCondition = strCondition.Substring(0, strCondition.Length - 4);

        }
        #endregion

        #region Property
        BaseDataOp Manager = new BaseDataOp();
       
        #endregion

  



  
    }
}
